﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.filters.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import fl.controls.*;
	import src.cenfun.*;
	public class Main extends Sprite {
		//中奖区总数
		private var total:Number = 60;
		//奖项
		private var award:Number;
		private var angle:Number = 0;
		private var pc:Number = 0xcccccc;
		private var pr:Number = 160;
		private var br:Number = 15;
		private var pan:Sprite = new Sprite();
		private var ball:Ball;
		private var msg:String = "Random: ";
		private var btAward:Button;
		private var btStart:Button;
		private var per:Per;
		public function Main() {
			randomAward();
			//
			pan.graphics.lineStyle(1, pc);
			pan.graphics.drawCircle(0, 0, pr);
			pan.graphics.drawCircle(0, 0, pr+2*br);
			pan.x = stage.stageWidth/2;
			pan.y = stage.stageHeight/2;
			addChild(pan);
			ball = new Ball(pr, 0, br, 0x0000ff);
			ball.x = stage.stageWidth/2;
			ball.y = stage.stageHeight/2;
			addChild(ball);
			//
			btAward = new Button();
			btAward.label = msg+award;
			btAward.addEventListener(MouseEvent.CLICK, awardHandler);
			btAward.move(5, 5);
			addChild(btAward);
			btStart = new Button();
			btStart.label = "Start";
			btStart.addEventListener(MouseEvent.MOUSE_DOWN, perStart);
			btStart.move(stage.stageWidth/2-btStart.width/2, stage.stageHeight/2-btStart.height/2);
			addChild(btStart);
			per = new Per(20, 100);
			per.move(stage.stageWidth/2, btStart.y);
			addChild(per);
			drawNumber();
		}
		private function perStart(e:Event):void{
			per.start();
			stage.addEventListener(MouseEvent.MOUSE_UP, ballStart);
		}
		private function ballStart(e:Event):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, ballStart);
			per.stop();
			btStart.enabled = false;
			//计算结束度数
			var end:Number = 360/total*(award-1)+360*(Math.round(per.percent*5)+1);
			var myTween:Tween = new Tween(ball, "rotation", Strong.easeOut, ball.rotation, end, 3+end/720, true);
			myTween.addEventListener("motionFinish", reStart);
		}
		private function awardHandler(e:Event):void {
			randomAward();
			btAward.label = msg+award;
		}
		private function randomAward():void {
			award = Math.round((total-1)*Math.random())+1;
			//trace(award);
		}
		private function reStart(e:Event):void {
			btStart.enabled = true;
		}
		private function drawNumber():void{
			var radian:Number = Math.PI/180;
			var tr:Number = pr + 3*br;
			for(var i:int = 0; i<total; i++){
				var label:TextField = new TextField();
				label.autoSize = "center";
				label.selectable = false;
				label.text = (i+1).toString();
				var d:Number = 360/total*i;
				var px:Number = stage.stageWidth/2 + tr*Math.cos(d*radian) - label.width/2;
				var py:Number = stage.stageHeight/2 + tr*Math.sin(d*radian) - label.height/2;
				label.x = px;
				label.y = py;
				addChild(label);
			}
		}
		private function getShadow(c:Number,a:Number):Array {
			return new Array(new DropShadowFilter(1,45,c,a,4,4,1,1));
		}
	}

}